<template>
    <div>
        <el-col :span="6" :offset="9" style="padding-top: 200px" >
            <el-form  :model="form" label-width="120px" class="edit" v-loading="loading" :rules="rules" ref="form">
                <el-form-item label="密码" prop="password">
                    <el-input type="password" auto-complete="off" v-model="form.password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" auto-complete="off" v-model="form.checkPass" placeholder="请输入重复密码" ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click.native.prevent="onSubmit('form')">更新</el-button>
                    <el-button @click="onCancel">取消</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </div>
</template>

<script>
    import { edit } from '../../../module/reader/reader/readerInfo'
    export default {
        data() {
            let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPass !== '') {
                        this.$refs.form.validateField('checkPass');
                    }
                    callback();
                }
            };
            let validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                form: {
                    id: "",
                    password: "",
                    checkPass: "",
                },
                loading: true,
                rules: {
                    password:  [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                }
            }
        },
        mounted () {
            let readerId = sessionStorage.getItem('readerId');
            if (readerId === undefined || readerId === null) {
                this.$router.push('/');
                return false;
            }
            this.form.id = readerId;
            this.loading = false;
        },
        methods: {
            onSubmit(form) {
                this.$refs[form].validate((valid) => {
                    if (valid) {
                        edit(this.form).then(data => {
                            this.$message({
                                type: 'success',
                                center: true,
                                message: '修改密码成功!'
                            });
                        });
                        this.$router.push({
                            name: "reader-book-entry"
                        })
                    } else {
                        return false;
                    }
                });
            },
            onCancel() {
                this.$router.push({
                    name: "reader-book-entry"
                })
            }
        }
    }


</script>